package com.seal.yoyoshop.adminweb;

import com.seal.yoyoshop.bin.Type;
import com.seal.yoyoshop.service.TypeService;
import com.seal.yoyoshop.service.impl.TypeServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "EditTypeServlet", value = "/admin/typeEdit")
public class EditTypeServlet extends HttpServlet {

    private TypeService typeService = new TypeServiceImpl();


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("flag",4);

        String idstr = request.getParameter("id");
        Integer id = Integer.parseInt(idstr);
        Type type = typeService.getTypeById(id);
        request.setAttribute("type",type);

        request.getRequestDispatcher("/admin/type_edit.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("UTF-8");
        String name = request.getParameter("name");
        String idstr = request.getParameter("id");
        Integer id = Integer.parseInt(idstr);

        Type type = typeService.getTypeById(id);
        type.setName(name);
        boolean isSuccess = typeService.updateTypeByType(type);
        if(isSuccess){
            request.setAttribute("msg","修改成功");
        }else request.setAttribute("msg","修改失败，商品大类名称过长");
        request.setAttribute("type",type);

        request.getRequestDispatcher("/admin/type_edit.jsp").forward(request,response);
    }
}
